﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TestOnlineProject.Models.Entities;

namespace TestOnlineProject.Models.DataAccess
{
    public class SectionDAO
    {
        private TestOnlineDBEntities db = new TestOnlineDBEntities();

        public List<Section> SelectBySubjectIdUserId(int subjectId, int userId)
        {
            try
            {
                var sections = db.Sections.Where(c => (c.SubjectId == subjectId && c.UserId == userId)).ToList();
                return sections;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<Section> SelectBySubjectId(int subjectId)
        {
            try
            {
                var sections = db.Sections.Where(c => (c.SubjectId == subjectId)).ToList();
                return sections;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public List<Section> Select()
        {
            try
            {
                var sections = db.Sections.ToList();
                return sections;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public Section SelectBySectionId(int sectionId)
        {
            try
            {
                var sections = db.Sections.Where(c => c.SectionId == sectionId).FirstOrDefault();
                return sections;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Insert(Section section)
        {
            try
            {
                db.Sections.AddObject(section);
                int result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Update(Section section)
        {
            try
            {
                int result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int Delete(int sectionId)
        {
            try
            {
                var section = db.Sections.Where(s => s.SectionId == sectionId).FirstOrDefault();
                section.SectionQuestions.ToList().ForEach(db.DeleteObject);
                db.Sections.DeleteObject(section);
                int result = db.SaveChanges();
                return result > 0 ? 1 : 0;
            }
            catch (Exception)
            {
                throw;
            }
        }

        public int DeleteByUserId(int userId)
        {
            try
            {
                var sections = db.Sections.Where(x => x.UserId == userId).ToList();
                foreach (var section in sections)
                {
                    db.Sections.DeleteObject(section);
                }
                var result = db.SaveChanges();
                return result;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        public List<Section> SelectByUserId(int userId)
        {
            var sections = db.Sections.Where(x => x.UserId == userId).ToList();
            return sections;
        }
    }
}